﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazzfire is open software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License (AGPL) as published by the Free Software Foundation, version 3 of the License.
// 
// Jazzfire is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public License along with Jetfire.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
# region using
using System.Linq;
using TrackerRealm.Jazz.Client;
using System.Collections.Generic;
using System;
using System.Reflection;
using TrackerRealm.Jazz.Common;
# endregion

namespace TrackerRealm.Jazz.WindowsForm
{
    public static class IEnumerableExtensions
    {
        # region ToReadableString
        /// <summary>
        /// Converts IEnumerable to a readable string
        /// </summary>
        /// <param name="values">jObject</param>
        /// <param name="pName">Property name used in the display</param>
        /// <returns></returns>
        public static string ToReadableString(this IEnumerable<jObject> values, string pName)
        {
            IEnumerable<string> sArray = values.Select(w => ReadString(w, pName));
            return String.Join<string>("; ", sArray);
        }
        /// <summary>
        /// Converts IEnumerable to a readable string
        /// </summary>
        /// <param name="values">jObject</param>
        /// <returns></returns>
        public static string ToReadableString(this IEnumerable<jAclItem> values)
        {
            IEnumerable<string> sArray = values.Select(w => String.Format("{0}{1}", w.Role.Name, w.Control == AccessControlType.ReadOnly ? " [Read-only]" : String.Empty));
            return String.Join<string>("; ", sArray);
        }
        /// <summary>
        /// Gets object from the jObject for the property name
        /// </summary>
        /// <param name="obj">jObject</param>
        /// <param name="pName">property Name</param>
        /// <returns></returns>
        private static string ReadString(jObject obj, string pName)
        {
            jMemberInfo mi = obj.Class.GetMembers().ToWrapper(obj).FirstOrDefault(m => m.Name == pName && m.CanRead);
            if (mi == null)
                return String.Empty;
            object o = mi.Read(obj);
            if (o != null)
                return o.ToString();
            return String.Empty;
        }
        # endregion
        # region ToListNames
        /// <summary>
        /// Converts IEnumerable to a readable string
        /// </summary>
        /// <param name="values">jObject</param>
        /// <param name="pName">Property Name</param>
        /// <returns></returns>
        public static List<string> ToListNames(this IEnumerable<jObject> values, string pName)
        {
            return values.Select(w => ReadString(w, pName)).ToList();
        }
        # endregion
    }
}